//
//  RootViewController.swift
//  Swift训练
//
//  Created by zhenjingxiong on 2018/8/30.
//  Copyright © 2018年 zhenjingxiong. All rights reserved.
//

import UIKit

class RootViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 添加子控制器
        addChildViewControllers()
    }
    
    func addChildViewControllers() {
        
       addChildViewController(childControllerName: "HomeViewController", title: "首页", imageName: "icon_home_normal", selectImageName: "icon_home_select")
        addChildViewController(childControllerName: "CustomerViewController", title: "客户", imageName: "icon_kehu_normal", selectImageName: "icon_kehu_selected")
        addChildViewController(childControllerName: "HousingViewController", title: "房源", imageName: "icon_fangyuan_normal", selectImageName: "icon_fangyuan_selected")
        addChildViewController(childControllerName: "MoreViewController", title: "更多", imageName: "icon_more_normal", selectImageName: "icon_more_selected")
    }
    
    func addChildViewController(childControllerName: String,title:String,imageName:String,selectImageName:String) {
        // 0.动态获取命名空间
        let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        print(namespace)
        
        // 告诉编译器暂时是AnyClass
        let cls:AnyClass =  NSClassFromString(namespace + "." + childControllerName)!
        // 告诉编译器真实类型是UIViewController
        let vcCls = cls as! UIViewController.Type
        
        //实例化控制器
        let vc = vcCls.init()
        
        //由内向外设置,nav和tabbar都有
        vc.title = title
        vc.tabBarItem.image = UIImage.init(named: imageName)
        vc.tabBarItem.selectedImage = UIImage.init(named: selectImageName)
        
        //注意:Xcode之前只有文字有效果，还需要设置图片渲染模式
        tabBar.tintColor = UIColor.orange
        
        //2.创建导航控制器
        let nav = CustomNavigationViewController.init()
        nav.addChildViewController(vc)
        
        //3.添加控制器到tabbarVC
        addChildViewController(nav)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
